Raster Fairy

Version 1.0
Author: Mario Klingemann | @Quasimondo

There are cases where distribution of the point cloud creates an non-optimum raster arrangement. By preprocessing the point coordinates you can try to get a better result. Here are a few examples:


In [6]:
# just some basic setup for the purpose of this demo:
%matplotlib inline  
from IPython.display import display
import matplotlib.pyplot as plt
import numpy as np


side = 85
n = side * side
np.random.seed(0)
xy = np.random.uniform(low=-1, high=+1, size=(n, 2))
xy = np.cumsum(xy, axis=0)
xy -= xy.min(axis=0)
xy /= xy.max(axis=0)
xy *= (side - 1)

totalDataPoints = len(xy)

colors = np.zeros((totalDataPoints,3))
colors[:,:2] += xy
colors-=np.min(colors)
colors/=np.max(colors)

fig = plt.figure(figsize=(10.0,10.0))
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('black')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.autoscale_view(True,True,True)
ax.invert_yaxis()
ax.scatter(xy[:,0],xy[:,1], c = colors,  edgecolors='none',marker='s',s=7.5)    
plt.show()



In [11]:
import rasterfairy

grid_xy, (width, height) = rasterfairy.transformPointCloud2D(xy)


fig = plt.figure(figsize=(10.0,10.0))
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('black')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.autoscale_view(True,True,True)
ax.invert_yaxis()
ax.scatter(grid_xy[:,0],grid_xy[:,1], c = colors,  edgecolors='none',marker='s',s=9)    
plt.show()



In [18]:
from rasterfairy import coonswarp

warped_xy = coonswarp.rectifyCloud(xy,perimeterSubdivisionSteps=4,autoPerimeterOffset=False, paddingScale=1.05)

fig = plt.figure(figsize=(10.0,10.0))
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('black')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.autoscale_view(True,True,True)
ax.invert_yaxis()
ax.scatter(warped_xy[:,0],warped_xy[:,1], c = colors,  edgecolors='none',marker='s',s=7.5)    
plt.show()

grid_xy, (width, height) = rasterfairy.transformPointCloud2D(warped_xy)


fig = plt.figure(figsize=(10.0,10.0))
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('black')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.autoscale_view(True,True,True)
ax.invert_yaxis()
ax.scatter(grid_xy[:,0],grid_xy[:,1], c = colors,  edgecolors='none',marker='s',s=9)    
plt.show()



In [23]:
warped_xy = coonswarp.rectifyCloud(xy,perimeterSubdivisionSteps=32,autoPerimeterOffset=True)

fig = plt.figure(figsize=(10.0,10.0))
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('black')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.autoscale_view(True,True,True)
ax.invert_yaxis()
ax.scatter(warped_xy[:,0],warped_xy[:,1], c = colors,  edgecolors='none',marker='s',s=7.5)    
plt.show()

grid_xy, (width, height) = rasterfairy.transformPointCloud2D(warped_xy)


fig = plt.figure(figsize=(10.0,10.0))
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('black')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.axes.get_xaxis().set_visible(False)
ax.axes.get_yaxis().set_visible(False)
ax.autoscale_view(True,True,True)
ax.invert_yaxis()
ax.scatter(grid_xy[:,0],grid_xy[:,1], c = colors,  edgecolors='none',marker='s',s=9)    
plt.show()



In [ ]: